<template>
  <div class="serve-home">
    <!-- 添加操作按钮 -->
    <div style="margin-bottom: 20px;">
      <el-button type="primary" @click="handleCreate">新增用户</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加用户对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="350px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>

    <el-pagination
        style="margin-top: 20px;"
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        @current-change="handlePageChange"
    ></el-pagination>
  </div>
</template>

<script>
import {
  getUserList,
  getUserDetail,
  createUser,
  updateUser,
  deleteUser} from "@/api/serve/home.js";

export default {
  data(){
    return {
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      dialogVisible: false,
      isCreate: true,
      dialogTitle: '新增用户',
      form: {
        id: null,
        name: '',
        email: ''
      }
    }
  },
  mounted() {
    this.getUserList()
    this.getUserDetail()
  },
  methods:{
    // 修改为获取用户列表
    async getUserList() {
      try {
        const res = await getUserList({
          page: this.currentPage,
          pageSize: this.pageSize
        });
        console.log(res,'获取用户列表');
        this.tableData = res.data;
        this.total = res.total;
      } catch (err) {
        console.error(err);
      }
    },

    // 修改为获取单个用户信息
    async getUserDetail() {
      try {
        const {data} = await getUserDetail({
          id: 100 // 这里可以根据需要动态传入用户ID
        });
        console.log(data,'获取单个用户信息');
      } catch (err) {
        console.error(err);
      }
    },

    handlePageChange(page) {
      this.currentPage = page;
      this.getUserList();
    },

    // 打开创建用户对话框
    handleCreate() {
      this.isCreate = true;
      this.dialogTitle = '新增用户';
      this.form = {
        id: null,
        name: '',
        email: ''
      };
      this.dialogVisible = true;
    },

    // 打开编辑用户对话框
    handleEdit(row) {
      this.isCreate = false;
      this.dialogTitle = '编辑用户';
      this.form = {
        id: row.id,
        name: row.name,
        email: row.email
      };
      this.dialogVisible = true;
    },

    // 删除用户
    async handleDelete(row) {
      try {
        await this.$confirm('确定要删除该用户吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteUser({ id: row.id })
        this.$message.success('删除成功')
        this.getUserList()
      } catch (err) {
        console.error(err)
        this.$message.error('删除失败')
      }
    },

    // 提交表单
    async submitForm() {
      try {
        if (this.isCreate) {
          await createUser({
            name: this.form.name,
            email: this.form.email
          });
          this.$message.success('创建成功');
        } else {
          await updateUser({
            id: this.form.id,
            name: this.form.name,
            email: this.form.email
          });
          this.$message.success('更新成功');
        }
        this.dialogVisible = false;
        this.getUserList();
      } catch (err) {
        console.error(err);
        this.$message.error('操作失败');
      }
    },
  }
}
</script>
<style scoped lang="scss">
.serve-home{
  padding:20px;
  :deep(.el-table) {
    border: 1px solid #ebeef5;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    th {
      background-color: #f5f7fa;
      color: #303133;
      font-weight: bold;
    }

    tr:hover {
      background-color: #f5f7fa;
    }

    .el-table__row--striped {
      background-color: #fafafa;
    }
  }
}
</style>
